// Copyright 2010 Google Inc. All Rights Reserved.

package com.google.zahra.webmanager;

import com.google.zahra.util.Util;
import com.google.zahra.workmanager.ZahraWorkManager;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author arthurkok@google.com (Your Name Here)
 *
 */

public class WorkManagerServlet extends HttpServlet {
  
  static final ZahraWorkManager workManager = WebManager.WORK_MANAGER;

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    response.setContentType("text/html");
    response.setStatus(HttpServletResponse.SC_OK);

    String command = request.getParameter("command");
    StringBuffer bodyString = new StringBuffer();
    
    bodyString.append("<h2>Work Manager Control Panel</h2>");

    if (command == null) {
      bodyString.append("<a href='/workManager?command=reset'>Reset workers</a><br/>");
    } else if (command.equals("reset")) {
      try {
        workManager.resetWorkers();
        bodyString.append("Reset workers successfully<br/>");
      } catch (InterruptedException e) {        
        bodyString.append("Reset workers failed<br/>");
        bodyString.append(e.getMessage());
        e.printStackTrace();
      }
    }
    response.getWriter().print(Util.getHTML(bodyString.toString(), "Zahra - Work Manager Control Panel"));
  }
}
